/**
 * Created by zengyx on 17/1/5.
 * 七牛上传工具封装
 */
// import * as qiniu from './qiniu-sdk'
import {Uploader, qiniuUpload} from './qiniuPluload'
import store from '@/store/index.js'
import { isUseQiniu } from './constance'
export let qiniu7moorDomain = 'https://fs-test.7moor.com/'
export let qiniu7moorImDomain = 'https://fs-im-kefu.7moor.com/'
export let qiniu7moorEmailDomain = 'https://fs-mail-resource.7moor.com/'
export let qiniu7moorKmDomain = 'https://fs-km.7moor.com/'
export let qiniu7moorRobotDomain = 'https://fs-km.7moor.com/'
export let qiniuUploadUrl = 'https://up.qbox.me'
export let cos7moorImDomain = 'https://v7-im-new-1256921685.cos.ap-beijing.myqcloud.com/'
export let cos7moorKmDomain = 'https://v7-im-new-1256921685.cos.ap-beijing.myqcloud.com/'

export let m7UpHosts = false

export const _initPrivateUploader = function (resp) {
  if (resp.version === 'private') {
    qiniu7moorDomain = resp.url_https
    qiniu7moorImDomain = resp.url_https
    qiniu7moorEmailDomain = resp.url_https
    qiniu7moorKmDomain = resp.url_https
    qiniu7moorRobotDomain = resp.url_https
    qiniuUploadUrl = resp.url_https
    if (window.location.protocol === 'https:') {
      m7UpHosts = resp.url_https.substring(8)
    } else {
      m7UpHosts = resp.url_http.substring(7)
    }
  }
}
// const Qiniu = new qiniu.QiniuJsSDK()
// const QiniuKm = new qiniu.QiniuJsSDK()
// const QiniuKmImg = new qiniu.QiniuJsSDK()
// const QiniuWebchat = new qiniu.QiniuJsSDK()
// const QiniuEmail = new qiniu.QiniuJsSDK()
// const QiniuRobot = new qiniu.QiniuJsSDK()

export let uploadTo7moorDomain = (preDomain, sessionId, buttonId, filters, fileAdded, beforeUpload, uploadProgress, uploadComplete, fileUploader, error, key, fileSize, multiSelection, isUseOld, data = {}, useContentType) => {
  let fileSizeData = '10mb'
  if (fileSize) {
    fileSizeData = fileSize
  }
  const useQiniu = isUseQiniu()
  return new Uploader({
    runtimes: 'html5',
    browse_button: buttonId,
    multi_selection: multiSelection || false,
    max_file_size: fileSizeData,
    dragdrop: true,
    chunk_size: 0,
    uptoken_url: {
      action: useQiniu ? 'app.weixin.getUptoken' : 'app.oss.getPresignedUrl',
      data
    },
    domain: qiniu7moorDomain,
    m7UpHosts: m7UpHosts,
    unique_names: false,
    save_key: false,
    get_new_uptoken: true,
    auto_start: true,
    filters: filters,
    init: {
      'FilesAdded': fileAdded,
      'BeforeUpload': beforeUpload,
      'UploadProgress': uploadProgress,
      'UploadComplete': uploadComplete,
      'FileUploaded': fileUploader,
      'Error': error,
      'Key': key
    },
    preDomain
  })
}

//  微信上传图片做了2mb的特殊限制，因此需要加上file_size参数
export let uploadTo7moorImDomain = (_, button, filters, fileAdded, beforeUpload, uploadProgress, uploadComplete, fileUploader, error, key, fileSize, multi) => {
  console.log(3333)
  let fileSizeData = '20mb'
  let number = 0
  if (fileSize) {
    fileSizeData = fileSize
  }
  const useQiniu = isUseQiniu()
  if (multi) number = multi.number
  // modify by liang.qi add return value
  return new Uploader({
    runtimes: 'html5',
    browse_button: button,
    multi_selection: multi,
    multi_number: number,
    max_file_size: fileSizeData,
    dragdrop: true,
    chunk_size: 0,
    uptoken_url: {
      action: useQiniu ? 'app.webchat.getImUptoken' : 'app.webchat.cosGetImUptoken',
      data: {}
    },
    domain: useQiniu ? (store.state.fileDomain.qiniu7moorImDomain || qiniu7moorImDomain) : (store.state.fileDomain.cos7moorImDomain || cos7moorImDomain),
    m7UpHosts: m7UpHosts,
    unique_names: false,
    save_key: false,
    get_new_uptoken: true,
    auto_start: true,
    useContentType: true,
    filters: filters,
    init: {
      'FilesAdded': fileAdded,
      'BeforeUpload': beforeUpload,
      'UploadProgress': uploadProgress,
      'UploadComplete': uploadComplete,
      'FileUploaded': fileUploader,
      'Error': error,
      'Key': key
    }
  })
}

export let PasteImguploadTo7moorKmDomain = (sessionId, file, filename, uploadProgress, uploadComplete, key, error, isUseOld) => {
  const useQiniu = isUseQiniu()
  console.log(file)
  qiniuUpload({
    uptoken_url: {
      action: useQiniu ? 'app.km.getKmUptoken' : 'app.km.cosGetKmUptoken',
      data: {}
    },
    m7UpHosts: m7UpHosts,
    file: file,
    key: key,
    fileName: filename,
    next: uploadProgress,
    error: error,
    complete: uploadComplete,
    useContentType: true,
    useQiniu
  })
}

export let uploadQRcode = (file, filename, uploadProgress, uploadComplete, key, error, domain, beforeUpload) => { // 二维码上传和工单附件上传
  const useQiniu = isUseQiniu()
  qiniuUpload({
    uptoken_url: {
      action: useQiniu ? 'app.weixin.getUptoken' : 'app.oss.getPresignedUrl',
      data: {
        scope: 'm7-common'
      }
    },
    m7UpHosts: m7UpHosts,
    file: file,
    key: key,
    fileName: filename,
    next: uploadProgress,
    error: error,
    complete: uploadComplete,
    useQiniu,
    domain,
    beforeUpload
  })
}

export let PasteImguploadTo7moorImDomain = (sessionId, button, file, filename, uploadProgress, uploadComplete, key, error, isUseOld) => {
  const useQiniu = isUseQiniu()
  qiniuUpload({
    uptoken_url: {
      action: useQiniu ? 'app.webchat.getImUptoken' : 'app.webchat.cosGetImUptoken',
      data: {}
    },
    m7UpHosts: m7UpHosts,
    file: file,
    key: key,
    fileName: filename,
    next: uploadProgress,
    error: error,
    complete: uploadComplete,
    unUseUploadOnload: true,
    useQiniu
  })
}

export let uploadTo7moorEmailDomain = (sessionId, button, filters, fileAdded, beforeUpload, uploadProgress, uploadComplete, fileUploader, error, key, fileSize, isUseOld) => {
  let fileSizeData = '10mb'
  if (fileSize) {
    fileSizeData = fileSize
  }
  const useQiniu = isUseQiniu()
  return new Uploader({
    runtimes: 'html5',
    browse_button: button,
    multi_selection: true,
    max_file_size: fileSizeData,
    dragdrop: true,
    chunk_size: 0,
    uptoken_url: {
      action: useQiniu ? 'app.webchat.getImUptoken' : 'app.webchat.cosGetImUptoken',
      data: {scope: 'm7-mail-resource'}
    },
    domain: useQiniu ? qiniu7moorEmailDomain : store.state.fileDomain.cos7moorEmailDomain,
    m7UpHosts: m7UpHosts,
    unique_names: false,
    save_key: false,
    get_new_uptoken: true,
    auto_start: true,
    filters: filters,
    init: {
      'FilesAdded': fileAdded,
      'BeforeUpload': beforeUpload,
      'UploadProgress': uploadProgress,
      'UploadComplete': uploadComplete,
      'FileUploaded': fileUploader,
      'Error': error,
      'Key': key
    }
  })
}
export let uploadTo7moorKmDomain = (sessionId, button, filters, fileAdded, beforeUpload, uploadProgress, uploadComplete, fileUploader, error, key, fileSize, isUseOld) => {
  const useQiniu = isUseQiniu()
  return new Uploader({
    runtimes: 'html5',
    browse_button: button,
    multi_selection: false,
    max_file_size: fileSize,
    dragdrop: true,
    chunk_size: 0,
    uptoken_url: {
      action: useQiniu ? 'app.km.getKmUptoken' : 'app.km.cosGetKmUptoken',
      data: {}
    },
    domain: useQiniu ? qiniu7moorKmDomain : store.state.fileDomain.cos7moorKmDomain,
    m7UpHosts: m7UpHosts,
    unique_names: false,
    save_key: false,
    get_new_uptoken: true,
    auto_start: true,
    filters: filters,
    init: {
      'FilesAdded': fileAdded,
      'BeforeUpload': beforeUpload,
      'UploadProgress': uploadProgress,
      'UploadComplete': uploadComplete,
      'FileUploaded': fileUploader,
      'Error': error,
      'Key': key
    },
    ctxTypeAutoCheck: true
  })
}
export let uploadTo7moorKmImgDomain = (sessionId, button, filters, fileAdded, beforeUpload, uploadProgress, uploadComplete, fileUploader, error, key, isUseOld) => {
  const useQiniu = isUseQiniu()
  return new Uploader({
    runtimes: 'html5',
    browse_button: button,
    multi_selection: false,
    max_file_size: '10mb',
    dragdrop: true,
    chunk_size: 0,
    uptoken_url: {
      action: useQiniu ? 'app.km.getKmUptoken' : 'app.km.cosGetKmUptoken',
      data: {}
    },
    domain: useQiniu ? qiniu7moorKmDomain : store.state.fileDomain.cos7moorKmDomain,
    m7UpHosts: m7UpHosts,
    unique_names: false,
    save_key: false,
    get_new_uptoken: true,
    auto_start: true,
    filters: filters,
    init: {
      'FilesAdded': fileAdded,
      'BeforeUpload': beforeUpload,
      'UploadProgress': uploadProgress,
      'UploadComplete': uploadComplete,
      'FileUploaded': fileUploader,
      'Error': error,
      'Key': key
    },
    ctxTypeAutoCheck: true
  })
}
export let uploadTo7moorlogoImgDomain = (sessionId, button, filters, fileAdded, beforeUpload, uploadProgress, uploadComplete, fileUploader, error, key, isUseOld) => {
  const useQiniu = isUseQiniu()
  return new Uploader({
    runtimes: 'html5',
    browse_button: button,
    multi_selection: false,
    max_file_size: '2mb',
    dragdrop: true,
    chunk_size: 0,
    uptoken_url: {
      action: useQiniu ? 'app.km.getKmUptoken' : 'app.km.cosGetKmUptoken',
      data: {}
    },
    domain: useQiniu ? qiniu7moorKmDomain : store.state.fileDomain.cos7moorKmDomain,
    m7UpHosts: m7UpHosts,
    unique_names: false,
    save_key: false,
    get_new_uptoken: true,
    auto_start: true,
    filters: filters,
    init: {
      'FilesAdded': fileAdded,
      'BeforeUpload': beforeUpload,
      'UploadProgress': uploadProgress,
      'UploadComplete': uploadComplete,
      'FileUploaded': fileUploader,
      'Error': error,
      'Key': key
    }
  })
}
export let uploadTo7moorRobotImgDomain = (sessionId, button, filters, fileAdded, beforeUpload, uploadProgress, uploadComplete, fileUploader, error, key, isUseOld) => {
  const useQiniu = isUseQiniu()
  return new Uploader({
    runtimes: 'html5',
    browse_button: button,
    multi_selection: false,
    max_file_size: '10mb',
    dragdrop: true,
    chunk_size: 0,
    uptoken_url: {
      action: useQiniu ? 'app.km.getKmUptoken' : 'app.km.cosGetKmUptoken',
      data: {}
    },
    domain: qiniu7moorRobotDomain,
    m7UpHosts: m7UpHosts,
    unique_names: false,
    save_key: false,
    get_new_uptoken: true,
    auto_start: true,
    filters: filters,
    init: {
      'FilesAdded': fileAdded,
      'BeforeUpload': beforeUpload,
      'UploadProgress': uploadProgress,
      'UploadComplete': uploadComplete,
      'FileUploaded': fileUploader,
      'Error': error,
      'Key': key
    }
  })
}
